package com.systop.asset.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.systop.asset.entity.AssetCategories;
import com.systop.asset.entity.vo.LocationVo;
import com.systop.asset.utils.TreeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.systop.asset.mapper.LocationMapper;
import com.systop.asset.entity.Location;
import com.systop.asset.service.ILocationService;

/**
 * 资产位置Service业务层处理
 * 
 * @author mc
 * @date 2025-06-25
 */
@Service
public class LocationServiceImpl implements ILocationService 
{
    @Autowired
    private LocationMapper locationMapper;

    /**
     * 查询资产位置
     * 
     * @param id 资产位置主键
     * @return 资产位置
     */
    @Override
    public Location selectLocationById(Long id)
    {
        return locationMapper.selectLocationById(id);
    }

    /**
     * 查询资产位置列表
     * 
     * @param location 资产位置
     * @return 资产位置
     */
    @Override
    public List<Location> selectLocationList(Location location)
    {
        return locationMapper.selectLocationList(location);
    }

    /**
     * 新增资产位置
     * 
     * @param location 资产位置
     * @return 结果
     */
    @Override
    public int insertLocation(Location location)
    {
        //名称不能重复
        Location location1 = locationMapper.selectLocationByName(location.getLocationName());
        if (location1 != null) {
            return 0;
        }
        return locationMapper.insertLocation(location);
    }

    /**
     * 修改资产位置
     * 
     * @param location 资产位置
     * @return 结果
     */
    @Override
    public int updateLocation(Location location)
    {
        //名称不能重复
        Location location1 = locationMapper.selectLocationByName(location.getLocationName());
        if (location1 != null && location1.getId()!=location.getId()) {
            return 0;
        }
        return locationMapper.updateLocation(location);
    }

    /**
     * 批量删除资产位置
     * 
     * @param ids 需要删除的资产位置主键
     * @return 结果
     */
    @Override
    public int deleteLocationByIds(Long[] ids)
    {
        return locationMapper.deleteLocationByIds(ids);
    }

    /**
     * 删除资产位置信息
     * 
     * @param id 资产位置主键
     * @return 结果
     */
    @Override
    public int deleteLocationById(Long id)
    {
        return locationMapper.deleteLocationById(id);
    }

    /**
     * 获取资产位置树形结构
     * @param location
     * @return
     */
    @Override
    public List<Location> getLocation(Location location) {
        List<Location> all = locationMapper.selectLocationList(location);
        return TreeUtils.buildTree(all);
    }
    /**
     * 获取所有资产位置数据
     * @param location
     * @return
     */
    public List<LocationVo> allList(Location location) {
        // 1. 获取所有资产分类数据
        List<Location> all = locationMapper.selectLocationList(location);

        // 2. 创建一个 Map 来存储每个节点的引用
        Map<Long, String> idToNameMap = new HashMap<>();
        for (Location newlocation : all) {
            idToNameMap.put(newlocation.getId(), newlocation.getLocationName());
        }

        // 3. 构造返回结果
        List<LocationVo> result = new ArrayList<>();
        for (Location newlocation : all) {
            LocationVo vo = new LocationVo();
            vo.setId(newlocation.getId());
            vo.setLocationName(newlocation.getLocationName());
            Long parentId = newlocation .getParentId();
            if (parentId == 0) {
                vo.setParentLocationName("-");
            } else {
                // 从map中直接获取父类名
                vo.setParentLocationName(idToNameMap.getOrDefault(parentId, "未知"));
            }

            result.add(vo);
        }

        return result;
    }
}
